<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
/**
 * Reports Controller
 *
 * @property Report $Report
 */
class MailinglistsController extends AppController {



/**
 * index method
 *
 * @return void
 */
         

	public function index() {
		$this->Mailinglist->recursive = 0;
		$this->set('mailinglists', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Mailinglist->exists($id)) {
			throw new NotFoundException(__('Invalid Mailing List'));
		}
		$options = array('conditions' => array('Mailinglist.' . $this->Mailinglist->primaryKey => $id));
		$this->set('mailinglist', $this->Mailinglist->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Mailinglist->create();
			if ($this->Mailinglist->save($this->request->data)) {
				$this->Session->setFlash(__('The Address record has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Address record could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Mailinglist->exists($id)) {
			throw new NotFoundException(__('Invalid Mailing List'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Mailinglist->save($this->request->data)) {
				$this->Session->setFlash(__('The Address record has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Address record could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Mailinglist.' . $this->Mailinglist->primaryKey => $id));
			$this->request->data = $this->Mailinglist->find('first', $options);
		}

	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Mailinglist->id = $id;
		if (!$this->Mailinglist->exists()) {
			throw new NotFoundException(__('Invalid Mailing List'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Mailinglist->delete()) {
			$this->Session->setFlash(__('The Address record is deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The Address record was not deleted'));
		$this->redirect(array('action' => 'index'));
	}

public function email() 
		{ 
				
		$this->set("mailinglist", $this->Mailinglist->find("all", array('order' => 'ListID'))); 
		$toList = array(); 
	
		if ($this->request->is('post') || $this->request->is('put')) 
		{
		($this->request->data);

		$email = new CakeEmail('gmail');
		$data = $this->request->data;

		
		$email->from('jmetoh@gmail.com');
		$email->to($data['Email']['To']);
		$email->subject($data['Email']['Subject']);
		
		$email->template('default');
		$email->emailFormat('html');

		
		foreach($this->data['Email']['dropdown'] as $i=>$j) 
		{ 
			$list = $this->Mailinglist->find('first', array('conditions' => array('ListID' => $j)));
			$email->addBcc($list['Mailinglist']['Email']);
		} 


		$email->send($data['Email']['Message']);

		/*
		$email->from('DKisnorbo@dfsadvisory.com.au'); 
		$email->subject($this->data['Email']['Subject']); 
		$email->send($this->data['Email']['Message']); 
		$email->template('default')
    	->emailFormat('html')
    	->send();
		} 
		else 
		{*/
	}
}


} 


